package com.gitee.kenewstar.migration.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gitee.kenewstar.migration.constants.Const;
import com.gitee.kenewstar.migration.entity.TemplateHeader;
import com.gitee.kenewstar.migration.entity.TemplateHeaderTl;
import com.gitee.kenewstar.migration.entity.TemplateLine;
import com.gitee.kenewstar.migration.entity.TemplateLineTl;
import com.gitee.kenewstar.migration.entity.TemplateTarget;
import com.gitee.kenewstar.migration.entity.TemplateTargetTl;
import com.gitee.kenewstar.migration.mapper.TemplateHeaderMapper;
import com.gitee.kenewstar.migration.mapper.TemplateHeaderTlMapper;
import com.gitee.kenewstar.migration.mapper.TemplateLineMapper;
import com.gitee.kenewstar.migration.mapper.TemplateLineTlMapper;
import com.gitee.kenewstar.migration.mapper.TemplateTargetMapper;
import com.gitee.kenewstar.migration.mapper.TemplateTargetTlMapper;
import com.gitee.kenewstar.migration.service.TemplateImportService;
import com.gitee.kenewstar.migration.util.Util;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author kenewstar
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class TemplateImportServiceImpl implements TemplateImportService {

    private final TemplateHeaderMapper templateHeaderMapper;
    private final TemplateHeaderTlMapper templateHeaderTlMapper;
    private final TemplateTargetMapper templateTargetMapper;
    private final TemplateTargetTlMapper templateTargetTlMapper;
    private final TemplateLineMapper templateLineMapper;
    private final TemplateLineTlMapper templateLineTlMapper;


    @Override
    @DS(Const.DEV)
    public List<TemplateHeader> devSelectHeader(List<String> tmplCodeList, Long tenantId) {
        if (CollUtil.isEmpty(tmplCodeList)) {
            return Collections.emptyList();
        }
        QueryWrapper<TemplateHeader> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("template_code", tmplCodeList);
        if (Objects.nonNull(tenantId)) {
            queryWrapper.eq(Const.TENANT_ID, tenantId);
        }
        return this.templateHeaderMapper.selectList(queryWrapper);
    }

    @Override
    @DS(Const.DEV)
    public List<TemplateHeader> devSelectHeaderList(List<TemplateHeader> list) {
        if (CollUtil.isEmpty(list)) {
            return Collections.emptyList();
        }
        Map<String, String> filterMap = list.stream().collect(Collectors.toMap(x -> x.getTemplateCode() + "-" + x.getTenantId(), TemplateHeader::getTemplateCode));
        QueryWrapper<TemplateHeader> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("template_code", list.stream().map(TemplateHeader::getTemplateCode).collect(Collectors.toSet()));
        queryWrapper.in(Const.TENANT_ID, list.stream().map(TemplateHeader::getTenantId).collect(Collectors.toSet()));
        List<TemplateHeader> lovList = this.templateHeaderMapper.selectList(queryWrapper);
        return lovList.stream().filter(x -> Objects.nonNull(filterMap.get(x.getTemplateCode() + "-" + x.getTenantId()))).collect(Collectors.toList());
    }

    @Override
    @DS(Const.DEV)
    public List<TemplateHeaderTl> devSelectHeaderTl(QueryWrapper<TemplateHeaderTl> queryWrapper) {
        return this.templateHeaderTlMapper.selectList(queryWrapper);
    }

    @Override
    @DS(Const.DEV)
    public List<TemplateTarget> devSelectTarget(QueryWrapper<TemplateTarget> queryWrapper) {
        return this.templateTargetMapper.selectList(queryWrapper);
    }

    @Override
    @DS(Const.DEV)
    public List<TemplateTargetTl> devSelectTargetTl(QueryWrapper<TemplateTargetTl> queryWrapper) {
        return this.templateTargetTlMapper.selectList(queryWrapper);
    }

    @Override
    @DS(Const.DEV)
    public List<TemplateLine> devSelectLine(QueryWrapper<TemplateLine> queryWrapper) {
        return this.templateLineMapper.selectList(queryWrapper);
    }

    @Override
    @DS(Const.DEV)
    public List<TemplateLineTl> devSelectLineTl(QueryWrapper<TemplateLineTl> queryWrapper) {
        return this.templateLineTlMapper.selectList(queryWrapper);
    }

    @Override
    @DS(Const.UAT)
    @Transactional(rollbackFor = Exception.class)
    public void uatInsertHeader(TemplateHeader header) {
        header.setTenantId(Util.getTenant(header.getTenantId()));
        this.templateHeaderMapper.insert(header);
    }

    @Override
    @DS(Const.UAT)
    @Transactional(rollbackFor = Exception.class)
    public void uatInsertHeaderTl(List<TemplateHeaderTl> list) {
        list.forEach(v -> v.setTenantId(Util.getTenant(v.getTenantId())));
        list.forEach(this.templateHeaderTlMapper::insert);
    }

    @Override
    @DS(Const.UAT)
    @Transactional(rollbackFor = Exception.class)
    public void uatInsertTarget(List<TemplateTarget> list) {
        list.forEach(v -> v.setTenantId(Util.getTenant(v.getTenantId())));
        list.forEach(this.templateTargetMapper::insert);
    }

    @Override
    @DS(Const.UAT)
    @Transactional(rollbackFor = Exception.class)
    public void uatInsertTargetTl(List<TemplateTargetTl> list) {
        list.forEach(v -> v.setTenantId(Util.getTenant(v.getTenantId())));
        list.forEach(this.templateTargetTlMapper::insert);
    }

    @Override
    @DS(Const.UAT)
    @Transactional(rollbackFor = Exception.class)
    public void uatInsertLine(List<TemplateLine> list) {
        list.forEach(v -> v.setTenantId(Util.getTenant(v.getTenantId())));
        list.forEach(this.templateLineMapper::insert);
    }

    @Override
    @DS(Const.UAT)
    @Transactional(rollbackFor = Exception.class)
    public void uatInsertLineTl(List<TemplateLineTl> list) {
        list.forEach(this.templateLineTlMapper::insert);
    }
}
